package com.qqclient.service;

import com.loy.entity.Message;
import com.loy.entity.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

/**
 * 1. 该类提供和消息相关的方法
 */
public class MessageClientService {

    /**
     * 1.
     * @param content  内容
     * @param senderId  发送者
     * @param getterId 接收者
     */
    public void sendMessageToOne(String content,String senderId,String getterId){

        Message message = new Message();
        message.setSender(senderId);
        message.setGetter(getterId);
        message.setContent(content);
        message.setSendTime(new java.util.Date().toString());
        message.setMesType(MessageType.MESSAGE_COMM_MES);
        System.out.println(senderId + "对" + getterId +"说：" + content);
        // 将消息发送给服务器
        try {
            OutputStream outputStream = ManageClientConnectServerThread.
                    getClientConnectServerThread(senderId)
                    .getSocket().getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outputStream);
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 1. 群发消息
     * @param content
     * @param senderId
     */
    public void sendMessageToAll(String content,String senderId ){
        Message message = new Message();
        message.setSender(senderId);
        message.setContent(content);
        message.setSendTime(new java.util.Date().toString());
        message.setMesType(MessageType.MESSAGE_TOALL_MES);
        System.out.println(senderId + "对大家说：" + content);
        // 将消息发送给服务器
        try {
            OutputStream outputStream = ManageClientConnectServerThread.
                    getClientConnectServerThread(senderId)
                    .getSocket().getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outputStream);
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
